package com.jiezuo.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.common.exception.BusinessRuntimeException;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.flowable.entity.MyForm;
import com.jiezuo.flowable.mapper.MyFormMapper;
import com.jiezuo.flowable.service.MyFormService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.FormService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.form.StartFormData;
import org.flowable.engine.form.TaskFormData;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @description 流程表单
 * @author 庞留杰
 * @date 2023/08/08 08:34
 * @version V1.0
 **/
@Transactional
@Slf4j
@Service
public class MyFormServiceImpl extends ServiceImpl<MyFormMapper, MyForm> implements MyFormService {

    @Autowired
    ProcessEngine processEngine;

    @Override
    public IPage<MyForm> findPage(Page initMpPage, MyForm myForm) {
        QueryWrapper<MyForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        return this.page(initMpPage, queryWrapper);
    }

    @Override
    public List<MyForm> findList(MyForm myForm) {
        QueryWrapper<MyForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<MyForm> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public MyForm findById(String id) {
        return this.getById(id);
    }

    @Override
    public boolean add(MyForm myForm) {
        return this.save(myForm);
    }

    @Override
    public boolean update(MyForm myForm) {
        return this.updateById(myForm);
    }

    @Override
    public boolean delete(String id) {
        MyForm myForm = new MyForm();
        myForm.setId(id);
        myForm.setIs_delete("1");
        return this.updateById(myForm);
    }

    @Override
    public MyForm getStartFormKey(String businessKey) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        FormService formService = processEngine.getFormService();
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(businessKey).orderByProcessDefinitionVersion().desc().list().get(0);
        String processDefinitionId = definition.getId();
        StartFormData formData = formService.getStartFormData(processDefinitionId);
        String formKey = formData.getFormKey();
        QueryWrapper<MyForm> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.eq("form_key", formKey);
        List<MyForm> list = this.list(queryWrapper);
        if (list.size() == 0) {
            throw new BusinessRuntimeException("未查询到对一个的表单");
        }
        return list.get(0);
    }

    @Override
    public MyForm getTaskFormKey(String taskId) {
        FormService formService = processEngine.getFormService();
        TaskFormData formData = formService.getTaskFormData(taskId);
        String formKey = formData.getFormKey();
        QueryWrapper<MyForm> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.eq("form_key", formKey);
        List<MyForm> list = this.list(queryWrapper);
        if (list.size() == 0) {
            throw new BusinessRuntimeException("未查询到对一个的表单");
        }
        return list.get(0);
    }

    @Override
    public String getFormKey(Map<String, String> map) {
        String processInstanceId = map.get("processInstanceId");
        String businessKey = map.get("businessKey");
        String state = map.get("state");//1申请，2待办，3已办
        String formKey = null;
        if ("1".equals(state)) {
            if (StringUtil.isNotEmpty(processInstanceId)) {
                Task task = this.processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                formKey = task.getFormKey();
            } else {
                ProcessDefinition definition = processEngine.getRepositoryService().createProcessDefinitionQuery()
                        .processDefinitionKey(businessKey).orderByProcessDefinitionVersion().desc().list().get(0);
                String processDefinitionId = definition.getId();
                StartFormData formData = processEngine.getFormService().getStartFormData(processDefinitionId);
                formKey = formData.getFormKey();
            }
        }
        if ("2".equals(state)) {
            Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
            formKey = task.getFormKey();
        }
        if ("3".equals(state)) {
            Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
            formKey = task.getFormKey();
        }
        return formKey;
    }

}
